package com.ruoyi.law.service.impl;

import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.law.domain.ContentCustomStep;
import com.ruoyi.law.domain.ContentExtend;
import com.ruoyi.law.mapper.ContentExtendMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.law.mapper.ContentCustomStepMapper;
import com.ruoyi.law.service.IContentCustomStepService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 自定义内容 步骤Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-02-25
 */
@Service
public class ContentCustomStepServiceImpl implements IContentCustomStepService 
{
    @Autowired
    private ContentCustomStepMapper contentCustomStepMapper;
    @Autowired
    private ContentExtendMapper contentExtendMapper;

    /**
     * 查询自定义内容 步骤
     * 
     * @param formId 自定义内容 步骤主键
     * @return 自定义内容 步骤
     */
    @Override
    public ContentCustomStep selectContentCustomStepByFormId(Long formId)
    {
        return contentCustomStepMapper.selectContentCustomStepByFormId(formId);
    }

    /**
     * 查询自定义内容 步骤列表
     * 
     * @param contentCustomStep 自定义内容 步骤
     * @return 自定义内容 步骤
     */
    @Override
    public List<ContentCustomStep> selectContentCustomStepList(ContentCustomStep contentCustomStep)
    {
        List<ContentCustomStep> contentCustomSteps = contentCustomStepMapper.selectContentCustomStepList(contentCustomStep);
        for (ContentCustomStep customStep : contentCustomSteps) {
           List<Map<String,Object>> options = contentExtendMapper.getOptions(customStep.getFormId());
            customStep.setOptions(options);
        }
        return contentCustomSteps;
    }

    /**
     * 新增自定义内容 步骤
     * 
     * @param contentCustomStep 自定义内容 步骤
     * @return 结果
     */
    @Override
    public int insertContentCustomStep(ContentCustomStep contentCustomStep)
    {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        contentCustomStep.setCreateTime(DateUtils.getNowDate());
        contentCustomStep.setCreateBy(sysUser.getUserName());
        List<Map<String, Object>> options = contentCustomStep.getOptions();
        int i = contentCustomStepMapper.insertContentCustomStep(contentCustomStep);
        if (options != null && options.size() > 0){
            for (Map<String, Object> option : options) {
                ContentExtend contentExtend = new ContentExtend();
                contentExtend.setExtendName(option.get("label").toString());
                contentExtend.setExtendValue(option.get("value").toString());
                contentExtendMapper.insertContentExtend(contentExtend);
            }
        }
        return i;
    }

    /**
     * 修改自定义内容 步骤
     * 
     * @param contentCustomStep 自定义内容 步骤
     * @return 结果
     */
    @Override
    public int updateContentCustomStep(ContentCustomStep contentCustomStep)
    {
        contentCustomStep.setUpdateTime(DateUtils.getNowDate());
        return contentCustomStepMapper.updateContentCustomStep(contentCustomStep);
    }

    /**
     * 批量删除自定义内容 步骤
     * 
     * @param formIds 需要删除的自定义内容 步骤主键
     * @return 结果
     */
    @Override
    public int deleteContentCustomStepByFormIds(Long[] formIds)
    {
        return contentCustomStepMapper.deleteContentCustomStepByFormIds(formIds);
    }

    /**
     * 删除自定义内容 步骤信息
     * 
     * @param formId 自定义内容 步骤主键
     * @return 结果
     */
    @Override
    public int deleteContentCustomStepByFormId(Long formId)
    {
        return contentCustomStepMapper.deleteContentCustomStepByFormId(formId);
    }
}
